/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.ufu.sdg12014.thriftpluresdb.server;

import br.ufu.sdg12014.base.clientreply.ThriftServer;
import java.util.ArrayList;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;

/**
 *
 * @author SergioPeres
 */
public class Server {
    public static ServerHandler handler;

    public static ThriftServer.Processor processor;
    
    public ArrayList<Server> participants;

    public static void main(String[] args) {
        if(args.length != 2)
        {
            System.err.println("SINTAXE: <> <File Name> <Length Values>");
            return;
        }        
        System.out.println("Iniciando servidor...");
        try {
            handler = new ServerHandler(args[0], Integer.parseInt(args[1]));
            processor = new ThriftServer.Processor(handler);

            Runnable simple = new Runnable() {
                public void run() {
                    simple(processor);
                }
            };

            new Thread(simple).start();
        } catch (Exception x) {
            x.printStackTrace();
        }
    }

    public static void simple(ThriftServer.Processor processor) {
        try {
            TServerTransport serverTransport = new TServerSocket(9090);
            TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor));

            System.out.println("Servidor iniciado com sucesso...\n");
            server.serve();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
